﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Serialization;

namespace Fis.Win.Utils
{
    public class UIUtils
    {
        /// <summary>
        /// 主窗口
        /// </summary>
        public static MainForm MainForm
        { get { return Application.OpenForms["MainForm"] as MainForm; } }

        private static DataSet viewConfig;
        public static DataSet ViewConfig
        {
            get
            {
                if (viewConfig == null)
                {
                    var path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", "view.Config");
                    viewConfig = new DataSet();
                    viewConfig.ReadXml(path);
                }

                return viewConfig;
            }
        }

        public static void InitializeNavBarControl(DevExpress.XtraNavBar.NavBarControl bar)
        {
            NavBarRegistry r = new NavBarRegistry();
            r.Initialize(bar);
            r.SetNavbarState();
        }

        public static void LookUpBinding(DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit edit, DataTable source, string display, string value)
        {
            edit.DataSource = source;
            edit.DisplayMember = display;
            edit.ValueMember = value;
            edit.ShowHeader = false;
            edit.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo(display));
            edit.AutoSearchColumnIndex = source.Columns.IndexOf(display);

        }

        public static void LookUpBinding(DevExpress.XtraEditors.LookUpEdit edit, DataTable source, string display, string value, object defaultValue = null)
        {
            edit.Properties.DataSource = source;
            edit.Properties.DisplayMember = display;
            edit.Properties.ValueMember = value;
            edit.Properties.ShowHeader = false;
            edit.Properties.Columns.Clear();
            edit.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo(display));
            edit.Properties.AutoSearchColumnIndex = source.Columns.IndexOf(display);
            if (defaultValue != null)
                edit.EditValue = defaultValue;
        }

        public static DevExpress.XtraGrid.GridControl CreateGridControl()
        {
            var gc = new DevExpress.XtraGrid.GridControl();

            var gv = new DevExpress.XtraGrid.Views.Grid.GridView(gc);
            gv.OptionsBehavior.Editable = false;
            gv.OptionsFilter.AllowColumnMRUFilterList = false;
            gv.OptionsMenu.EnableColumnMenu = false;
            gv.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;
            gv.OptionsView.ShowGroupPanel = false;

            gc.MainView = gv;
            gc.Dock = DockStyle.Fill;
            return gc;
        }

        public static void CreateColumn(DevExpress.XtraGrid.Views.Grid.GridView view, string caption, string field, int index, bool visible = true)
        {
            var column = view.Columns.AddVisible(field, caption);
            column.Visible = visible;
            column.VisibleIndex = index;
            column.SortMode = DevExpress.XtraGrid.ColumnSortMode.Value;

        }

        public static void ClearBindings(ContainerControl cc)
        {
            foreach (var edit in cc.Controls)
            {
                var c = (edit as DevExpress.XtraEditors.BaseEdit);
                if (c != null) c.DataBindings.Clear();
            }

        }

        public static void TagBindings(ContainerControl cc, object datasource)
        {
            foreach (var edit in cc.Controls)
            {
                var c = (edit as DevExpress.XtraEditors.BaseEdit);
                if (c != null && !Agile.Engine.Utils.StringUtils.IsEmpty(c.Tag))
                {
                    c.DataBindings.Add("EditValue", datasource, c.Tag.ToString());
                }
            }

        }

        public static string ToFilterString(DevExpress.XtraEditors.BaseEdit edit, string format, string value = "")
        {
            if (!Agile.Engine.Utils.StringUtils.IsEmpty(edit.EditValue))
            {
                return string.Format(format, string.IsNullOrEmpty(value) ? edit.EditValue : value);
            }

            return string.Empty;
        }

        /// <summary>
        /// 创建菜单项
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="manager"></param>
        /// <param name="bar"></param>
        /// <param name="caption"></param>
        /// <param name="id"></param>
        /// <param name="handle"></param>
        /// <returns></returns>
        public static T CreateBarItem<T>(DevExpress.XtraBars.BarManager manager
            , DevExpress.XtraBars.Bar bar, string caption, int id
            , DevExpress.XtraBars.ItemClickEventHandler handle = null
            , DevExpress.XtraBars.BarItemLinkAlignment align = DevExpress.XtraBars.BarItemLinkAlignment.Left
            , DevExpress.XtraBars.BarItemVisibility visible = DevExpress.XtraBars.BarItemVisibility.Always
            )
            where T : DevExpress.XtraBars.BarItem, new()
        {
            var item = new T() { Caption = caption, Id = id, Visibility = visible, Alignment = align };
            manager.Items.Add(item);
            
            var insert = (id > 0) ? id - 1 : 0;
            bar.InsertItem(bar.ItemLinks[insert], item);
            bar.LinksPersistInfo.Add(new DevExpress.XtraBars.LinkPersistInfo(item));
            if (handle != null)
                item.ItemClick += handle;
            return item;
        }

    }
}
